﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace SuperX.DataService
{
    public class TreeHelper
    {
        /// <summary>
        /// 双层循环
        /// </summary>
        /// <param name="treeNodes"></param>
        /// <returns></returns>
        public static List<BaseTreeNode> BulidTree(List<BaseTreeNode> treeNodes)
        {
            try
            {
                List<BaseTreeNode> trees = new List<BaseTreeNode>();

                foreach (var treeNode in treeNodes)
                {
                    if ("0" == (treeNode.ParentId))
                    {
                        trees.Add(treeNode);
                    }

                    foreach (var it in treeNodes)
                    {
                        if (it.ParentId == treeNode.Id)
                        {
                            treeNode.Children.Add(it);
                        }
                    }
                }
                return trees;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 使用递归方法建树
        /// </summary>
        public static List<BaseTreeNode> BulidTreeByRecursive(List<BaseTreeNode> treeNodes, List<BaseTreeNode> resps, string pID)
        {
            resps = new List<BaseTreeNode>();
            List<BaseTreeNode> tempList = treeNodes.Where(c => c.ParentId == pID).ToList();

            for (int i = 0; i < tempList.Count; i++)
            {
                BaseTreeNode node = new BaseTreeNode();
                node.Id = tempList[i].Id;
                node.ParentId = tempList[i].ParentId;
                node.Name = tempList[i].Name;
                node.PreDriverName = tempList[i].PreDriverName;
                node.AssemblyName = tempList[i].AssemblyName;
                node.ClassFullName = tempList[i].ClassFullName;
                node.Argument = tempList[i].Argument;
                node.AddressExtend = tempList[i].AddressExtend;
                node.Children = BulidTreeByRecursive(treeNodes, resps, node.Id);
                resps.Add(node);
            }
            return resps;
        }

        public static List<TreeModel> BulidTreeByRecursive(List<TreeModel> treeNodes, List<TreeModel> resps, string pID)
        {
            resps = new List<TreeModel>();
            List<TreeModel> tempList = treeNodes.Where(c => c.Parentkey == pID).ToList();

            for (int i = 0; i < tempList.Count; i++)
            {
                TreeModel node = new TreeModel();
                node.Key = tempList[i].Key;
                node.Parentkey = tempList[i].Parentkey;
                node.Name = tempList[i].Name;
                node.Children = tempList[i].Children;
                node.SystemType = tempList[i].SystemType;
                node.Type = tempList[i].Type;
                node.CreatedByTemplate = tempList[i].CreatedByTemplate;
                node.TemplateSync = tempList[i].TemplateSync;
                node.ConfigKey = tempList[i].ConfigKey;
                node.ConectState = tempList[i].ConectState;
                node.AddressExtend = tempList[i].AddressExtend;
                node.ConfigParentkey = tempList[i].ConfigParentkey;
                node.Children = BulidTreeByRecursive(treeNodes, resps, node.Key);
                resps.Add(node);
            }
            return resps;
        }
    }
}